<template>
  <el-dialog :title="'分配角色：'+username" :visible="isShowRole" @close="btncancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="role in roleList"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button @click="btncancel">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </el-row>
    </template>

  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailByIdApi } from '@/api/user'
import { assignRoles } from '@/api/employees'

export default {
  props: {
    isShowRole: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleList: [],
      checkList: [],
      userId: '',
      username: ''
    }
  },
  async created() {
    const { rows } = await getRoleList({
      page: 1,
      pagesize: 999
    })
    this.roleList = rows
  },
  methods: {
    async btnOK() {
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      // console.log(res)
      this.$message.success('修改成功')
      // 关闭窗口
      // 用户update: 就不用在父组件从新定义, 只要在父组件中(想要修改的加上 .sync)
      // :is-show-role.sync="isShowRole"
      // , false 是要改成的值
      this.$emit('update:isShowRole', false)
      // this.$parent.isShowRole = false
    },

    btncancel() {
      // 关闭窗口
      this.$parent.isShowRole = false
    },
    async getCheckList(id) {
      this.userId = id
      const res = await getUserDetailByIdApi(id)
      // console.log(res)
      this.checkList = res.roleIds
      this.username = res.username
      // console.log(id)
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
